﻿using System;
using System.IO;
using EmailJabber.EmailJabber.Core.FileManagemant;
using EmailJabber.Logic.Commands.Base;
using EmailJabber.Logic.Logic.BusinessLogic;

namespace EmailJabber.Logic.Commands.FileViewCommands
{
    public class ViewFileCommand:CommandBase
    {

        protected override string GetDecription()
        {
            return "View text files";
        }

        protected override Guid GetId()
        {
            return new Guid("F4A12C21-FCFB-4E50-905B-C003D43E4140");
        }

        protected override void ExecuteSafe(MessageIM comnd)
        {
            string result = "oops! :(";
            
            if(File.Exists(comnd.Body))
            {

                using (var sr = new StreamReader(comnd.Body, FileSystemBlo.Instance.CurrentEncoding))
                {
                    result = sr.ReadToEnd();
                } 
            }
            else if(File.Exists( string.Format("{0}\\{1}", FileSystemBlo.Instance.CurrentDir, comnd)))
            {
                string filename = string.Format("{0}\\{1}", FileSystemBlo.Instance.CurrentDir, comnd);
                using (var sr = new StreamReader(filename, FileSystemBlo.Instance.CurrentEncoding))
                {
                    result = sr.ReadToEnd();
                } 
            }
            
            BL.Instance.Send(MessageIM.GetReply(result,comnd));
        }
    }
}